package com.hp.test.java.io.file;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileUtils {

    public static List<String> readFileByLines(String fileName) {
        BufferedReader reader = null;
        List<String> list = new ArrayList<String>();

        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");

            File file = new File(fileName);
            reader = new BufferedReader(new FileReader(file));

            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                list.add(tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }

    public static String readFileContent(String fileName) {
        String content = null;

        //第一步 获取通道
        FileInputStream fis = null;
        FileChannel channel = null;
        try {
            fis = new FileInputStream(fileName);
            channel = fis.getChannel();
            //文件内容的大小
            int size = (int) channel.size();

            //第二步 指定缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //第三步 将通道中的数据读取到缓冲区中
            channel.read(buffer);

            buffer.flip();
            content = new String(buffer.array(), 0, size);

            buffer.clear();
            buffer = null;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
                fis.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return content;
    }

    public static void writeFileByLines(String fileName, String line) {
        RandomAccessFile randomAccessFile = null;
        FileChannel channel = null;

        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }

            randomAccessFile = new RandomAccessFile(file, "rws");
            channel = randomAccessFile.getChannel();
            channel.write(ByteBuffer.wrap((line + "\r\n").getBytes("UTF-8")), channel.size());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 导出CSV文件
     *
     * @param fileName
     * @param dataList
     * @return
     */
    public static boolean exportCsv(String fileName, List<String> dataList) {
        boolean isSucess = false;

        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            File file = new File(fileName);
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }

            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out, "gbk");
            bw = new BufferedWriter(osw);
            if (dataList != null && !dataList.isEmpty()) {
                for (String data : dataList) {
                    bw.append(data).append("\r\n");
                }
            }
            isSucess = true;
        } catch (Exception e) {
            isSucess = false;
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                    bw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                    osw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

    /**
     * 导入CSV文件
     *
     * @param fileName
     * @return
     */
    public static List<String> importCsv(String fileName) {
        File file = new File(fileName);
        if (!file.isFile()) {
            return Collections.emptyList();
        }

        return importCsv(file);
    }

    /**
     * 导入CSV文件
     *
     * @param file
     * @return
     */
    public static List<String> importCsv(File file) {
        List<String> dataList = new ArrayList<String>();

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return dataList;
    }

    /**
     * 导入 固定编码的CSV文件
     *
     * @param file
     * @param charset
     * @return
     * @throws IOException
     */
    public static List<String> importCsvChar(String file, String charset) throws IOException {
        List<String> lines = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));

        String line = null;
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }

        br.close();

        return lines;
    }

    public static List<String> txt2String(File file) {
        BufferedReader br = null;
        List<String> list = new ArrayList<String>();
        List<String> merchantIds = new ArrayList<String>();

        try {
            br = new BufferedReader(new FileReader(file));
            String s = null;
            while ((s = br.readLine()) != null) {
                merchantIds.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    public static String readFileToString(File file, Charset charset) throws FileNotFoundException, IOException {
        BufferedReader d = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset.name()));
        java.lang.StringBuffer content = new StringBuffer();
        String line = "";
        while ((line = d.readLine()) != null) {
            content.append(line + "\n");
        }

        d.close();

        return new String(content);
    }

    public static void main(String[] args) {
        System.out.println(readFileContent("D:\\merchantid 2.txt"));
    }

}
